package com.q.tqb.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.RedCommentInfo;
import com.q.tqb.util.SPUtil;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RedCommentAdapter extends RecyclerView.Adapter<RedCommentAdapter.ViewHolder> {
    private ArrayList<RedCommentInfo> mRedCommentInfos;
    private Context mContext;
    private String uid;


    public RedCommentAdapter(Context context, ArrayList<RedCommentInfo> mRedCommentInfos) {
        this.mRedCommentInfos = mRedCommentInfos;
        this.mContext = context;
        this.uid = String.valueOf((int) SPUtil.get(mContext, "uid", 0));
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_red_comment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final RedCommentInfo redCommentInfo = mRedCommentInfos.get(position);

        Glide
                .with(mContext)
                .load(redCommentInfo.getHeader())
                .error(R.drawable.ic_head)
                .placeholder(R.drawable.ic_head)
                .dontAnimate()
                .into(holder.mIvHead);

        //    if (redCommentInfo.getUid().equals(uid)) {
        holder.mTvBack.setVisibility(View.VISIBLE);
//        } else {
//            holder.mTvBack.setVisibility(View.GONE);
//        }
        holder.mTvName.setText(redCommentInfo.getNickname());
        holder.mTvContent.setText(redCommentInfo.getComment());
        holder.mTvTime.setText(redCommentInfo.getAdd_time());


        holder.mTvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onBackClick(redCommentInfo.getCmid());
            }
        });

        holder.mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onHeadClick(redCommentInfo.getUid());
            }
        });


        if (redCommentInfo.getSComent() != null) {
            int size = redCommentInfo.getSComent().size();
            TextView textView;
            holder.mLlBack.removeAllViews();
            for (int i = 0; i < size; i++) {
                textView = new TextView(mContext);
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.text_black));
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                RedCommentInfo.SComentBean sComentBean = redCommentInfo.getSComent().get(i);
                textView.setText(sComentBean.getNickname() + ":  " + sComentBean.getComment());
                holder.mLlBack.addView(textView);
            }
            holder.mLlBack.setVisibility(View.VISIBLE);
        } else {
            holder.mLlBack.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return mRedCommentInfos.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_head)
        CircleImageView mIvHead;
        @BindView(R.id.tv_name)
        TextView mTvName;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_back)
        TextView mTvBack;
        @BindView(R.id.ll_back)
        LinearLayout mLlBack;


        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onBackClick(String uid);

        void onHeadClick(int uid);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


}